﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Leo.Entities;

namespace Leo.Repository
{
    public class LoteRepository: MasterRepository, ILoteRepossitory
    {
        public List<Lote> GetAllFromLote()
        {
            var query = from p in context.Lotes
                        select p;

            return query.ToList();
        }

        public List<Lote> GetFromLoteByCriterio(string criterio)
        {
            var query = from p in context.Lotes
                        select p;
            if (criterio != "" & criterio != null & criterio != string.Empty)
            {
                query = from p in query
                        where p.Descripcion.ToUpper().Contains(criterio.ToUpper())

                        select p;
            }

            return query.ToList();
        }

        public Lote GetFromLoteById(int id)
        {
            return context.Lotes.Find(id);
        }

        public void AddLote(Lote lote)
        {
            context.Lotes.Add(lote);
            context.SaveChanges();
        }

        public void UpdateLote(Lote lote)
        {
            context.Entry(lote).State = System.Data.EntityState.Modified;
            context.SaveChanges();
        }

        public void RemoveLote(int id)
        {
            var existe = context.Lotes
                        .Where(p => p.Id.Equals(id))
                        .SingleOrDefault();

            if (existe != null)
            {
                context.Lotes.Remove(existe);
                context.SaveChanges();
            }
        }

        public bool ExisteDuplicidad(string descripcion, int id)
        {
            var query = from p in context.Lotes
                        where p.Descripcion.ToUpper().CompareTo(descripcion.ToUpper()) == 0  && p.Id != id
                        select p;

            return query.Any();
        }
    }
}
